package com.social.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.social.dao.VolunteerDao;
import com.social.pojo.Result;
import com.social.pojo.VolunteerRegister;
import com.social.pojo.Volunteering;
import com.social.service.VolunteerRegisterService;
import com.social.service.VolunteerService;
import com.social.utils.UserHolder;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.social.constant.MessageConstant.*;

/**
 * @className: VolunteerServiceImpl
 * @description: TODO 类描述
 * @author: XiaoXu
 * @date: 2022/11/28 13:29
 **/
@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerDao, Volunteering> implements VolunteerService {
    private VolunteerRegisterService volunteerRegisterService;

    @Autowired
    public void setVolunteerRegisterService(VolunteerRegisterService volunteerRegisterService){
        this.volunteerRegisterService = volunteerRegisterService;
    }

    @Override
    public Result modify(Volunteering volunteering) {
        boolean isSuccess = updateById(volunteering);
        if(!isSuccess){
            return Result.fail(UPDATE_DATA_FAIL,"修改失败");
        }
        return Result.ok(OPERATE_DATA_SUCCESS);
    }

    @Override
    public Result insert(Volunteering volunteering) {
        boolean isSuccess = save(volunteering);
        if(!isSuccess){
            return Result.fail(SAVA_DATA_FAIL,"添加失败");
        }
        return Result.ok(OPERATE_DATA_SUCCESS);
    }

    @Override
    public Result getVolunteerById(Integer id) {
        Volunteering one = getById(id);
        if(one == null){
            return Result.fail(QUERY_DATA_FAIL,"志愿活动不存在");
        }
        return Result.ok(OPERATE_DATA_SUCCESS,one);
    }

    @Override
    public Result register(Long id) {

        return null;
    }

    public Integer isRegister(Integer id) {
        VolunteerRegister one = volunteerRegisterService.query().eq("activityId", id).eq("userId", UserHolder.getUser().getId()).one();
        if(one != null){
            return one.getState();
        }
        return null;
    }
}
